package com.mall.service.impl;

import com.mall.dao.IMemberDao;
import com.mall.domain.Member;
import com.mall.domain.Page;
import com.mall.service.IMemberService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会员的业务层实现类
 * @author : Bei-Zhen
 * @date : 2020-12-30 11:48
 */
@Service("memberService")
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private IMemberDao memberDao;

    /**
     * 查询所有会员
     * @param page 分页实体类
     * @return
     */
    @Override
    public List<Member> findAll(Page page){
        //判断是否为模糊查询
        if(page.getQuery() == null){
            page.setQuery("%");
        } else {
            page.setQuery("%" + page.getQuery() + "%");
        }
        //设置分页查询开始值
        page.setNum((page.getNum() - 1) * page.getSize());
        return memberDao.findAll(page);
    }

    /**
     * 查询所有会员数量
     * @return
     */
    @Override
    public Integer findTotal(){
        return memberDao.findTotal();
    }

    /**
     * 更新会员
     * @param member 会员实体类
     * @return
     */
    @Override
    public Integer update(Member member){
        member.setUpdated(new Date());
        return memberDao.update(member);
    }

    /**
     * 添加会员
     * @param member 会员实体类
     * @return
     */
    @Override
    public Integer add(Member member){
        //默认会员积分为0，会员状态可用
        member.setIntegral(0);
        member.setStatus(true);
        member.setCreated(new Date());
        return memberDao.add(member);
    }

    /**
     * 删除会员
     * @param id 会员编号
     * @return
     */
    @Override
    public Integer del(Integer id){

        return memberDao.del(id);
    }

    @Override
    public Member login(Member loginUser) {
        return memberDao.login(loginUser);
    }

    @Override
    public List<Member> findMemberById(Integer id) {
        return memberDao.findMemberById(id);
    }
}
